﻿using System;
using System.Collections.Generic;
using System.Web;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Constants;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Engine;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Entities;

namespace kunukn.RecommendationEngine.AreaRecommendation.Code.Helpers
{
    /// <summary>
    /// Author: Kunuk Nykjaer
    /// </summary>
    public static class SessionHelper
    {        
        public const int ApplicationIdlenth = 8;

        # region  :: Session ::

        public static string GetUserId()
        {
            return GetUserSession().Id;
        }

        public static List<Item> GetViewedItems()
        {
            var obj = HttpContext.Current.Session[SessionKeys.ViewedItems] as List<Item>;
            if (obj == null)
            {
                obj = new List<Item>();
                HttpContext.Current.Session[SessionKeys.ViewedItems] = obj;
            }
            return obj;
        }
        public static void ResetViewedItems()
        {
            HttpContext.Current.Session[SessionKeys.ViewedItems] = new List<Item>();
        }

        public static bool IsSkipNextTracking()
        {
            var s = HttpContext.Current.Session[SessionKeys.SkipNextTrackEvent] as string;
            if (s != null && s == Tracker.SkipMode)
            {
                HttpContext.Current.Session[SessionKeys.SkipNextTrackEvent] = null;
                return true;
            }
            return false;
        }
        public static void SetSkipNextTrackingEvent(string mode = null)
        {
            HttpContext.Current.Session[SessionKeys.SkipNextTrackEvent] = mode ?? Tracker.SkipMode;
        }
        
        public static Item2Item GetItem2Item()
        {
            var obj = HttpContext.Current.Session[SessionKeys.I2I] as Item2Item;
            if(obj==null)
            {
                obj = new Item2Item();
                HttpContext.Current.Session[SessionKeys.I2I] = obj;
            }                        
            return obj;
        }

        public static UserSession GetUserSession()
        {
            var obj = HttpContext.Current.Session[SessionKeys.UserSession] as UserSession;
            if (obj == null)
            {
                obj = new UserSession();                
                HttpContext.Current.Session[SessionKeys.UserSession] = obj;
            }
            return obj;
        }

        public static string GetDebugData()
        {
            return HttpContext.Current.Session[SessionKeys.Debug] as string;
        }
        public static void SetDebugData(string s)
        {
            HttpContext.Current.Session[SessionKeys.Debug] = s;
        }

        # endregion  :: Session ::

        # region  :: Application ::

        public static List<Item> GetItems()
        {
            var items = HttpContext.Current.Application[SessionKeys.Items] as List<Item>;
            if (items == null)
            {
                throw new ApplicationException("App session storage GetItems() items is null");
            }
                
            return items;
        }

        public static Dictionary<string, int> GetLookupNameToIndex()
        {
            var obj = HttpContext.Current.Application[SessionKeys.LookupNameToIndex] as Dictionary<string, int>;
            if (obj == null)
            {
                throw new ApplicationException("LookupNameToIndex not instantiated");
            }
                
            return obj;
        }

        public static int GetItemIndex(this Item item)
        {
            if (!item.IsValid())
            {
                throw new ApplicationException(string.Format("GetItemIndex() item is invalid: {0}", item));
            }                

            return GetItemIndex(item.Name);
        }
        public static int GetItemIndex(this string name)
        {
            if (name.IsValidaItemName())
            {
                return GetLookupNameToIndex()[name];
            }
                
            throw new ApplicationException(string.Format("GetItemIndex() name is invalid: {0}", name));
        }
    
        # endregion
    }
}